import Swiper from 'swiper/dist/js/swiper.min'

$(async () => {
  // 初始化Swiper
  new Swiper('.swiper-container', {
    // Optional parameters
    direction: 'horizontal',
    loop: true,

    // Auto play
    autoplay: {
      delay: 3000,
    },

    // If we need pagination
    pagination: {
      el: '.swiper-pagination',
    }
  })

  const campaign = 'invite-one-result'
  const productId = window.campaignProduct
  console.log(productId)
  let orderUrl = `${window.location.protocol}//${window.location.host}/wx/2order${Math.ceil(Math.random() * 100)}`
  const av = 'p014'
  utils.initAnalytics(av)
  analytics.send({
    event: `${av}_open`,
    attr: {
      _c: utils.getUrlParam('_c')
    }
  })
  utils.initWechatShare()
  const account = new Account()
  utils.supportPromise(account)
  const userInfo = await account.$getUserInfo()
  console.log(userInfo)
  const rid = utils.getUrlParam('rid') || ''
  const inviteInfo = await account.$ajax({
    auth: userInfo ? true : false,
    type: 'GET',
    path: `/s/events/inviteOneOnOne/${userInfo ? 'code' : 'public'}?code=${rid}`
  })
  console.log(inviteInfo)
  const [inviteUserInfo] = await account.$ajax({
    auth: false,
    type: 'GET',
    path: `/s/students/socials?ids=${inviteInfo.studentId}`
  })
  console.log(inviteUserInfo)
  if (inviteUserInfo) {
    $('.avatar').attr('src', inviteUserInfo.avatar || `${RESOURCE}/img/avatar.png?v=${VERSION}`)
    $('.nickname').text(inviteUserInfo.nickname)
    if (inviteInfo.status === 0 || inviteInfo.status === 3) {
      $('.apply-now').text(inviteInfo.status === 3 ? '您已领取过福利' : '0元领取福利')
      orderUrl += `?${productId ? `pid=${productId}` : ''}${campaign ? `&_c=${campaign}` : ''}${userInfo ? `&_r=${userInfo.shareFlag}` : ''}${rid ? `&rid=${rid}`: ''}`
      utils.initWechatShare({
        title: `好友${inviteUserInfo.nickname}送你3节久趣1对1北美外教课，价值450元！`,
        desc: '100%北美外教，让4-12岁孩子爱上英语！',
        link: `${window.location.protocol}//${window.location.host}/wx/invite-one-result${Math.ceil(Math.random() * 100)}?${campaign ? `&_c=${campaign}` : ''}${userInfo ? `&_r=${userInfo.shareFlag}` : ''}${rid ? `&rid=${rid}`: ''}`,
        imgUrl: 'https://static-app.97kid.com/site-student/public/mobile/campaign/inviteOne/icon/ic_share.png',
        success: target => {
          analytics.send({
            event: `${av}-share`,
            attr: {target}
          })
        }
      })
    } else {
      $('.apply-now').text(inviteInfo.status === 1 ? '已全部赠送' : '特权已过期')
      orderUrl = ''
      utils.initWechatShare({
        title: '【5折福利】久趣1对1北美精英外教在线教学，仅500名额！',
        desc: '适合4-12岁，专属外教固定时间，让孩子爱上英语！',
        link: `${window.location.protocol}//${window.location.host}/wx/2one${Math.ceil(Math.random() * 100)}?_c=${campaign}&_r=${userInfo.shareFlag}`,
        imgUrl: 'https://static-app.97kid.com/site-student/public/mobile/campaign/1v1/img/share.jpg',
        success: target => {
          analytics.send({
            event: `${av}-share`,
            attr: {target}
          })
        }
      })
    }
  }

  $('.apply-now').off('click').on('click', e => {
    if (orderUrl) window.location.href = orderUrl
  })
})